<template>
  <div class="user-container">
    <!-- 用户基本信息面板 -->
    <div class="user-card">
      <!-- 用户头像、姓名 -->
      <van-cell class="avatar-pd">
        <!-- 使用 title 插槽来自定义标题 -->
        <template #icon>
          <img :src="userAvatar" alt="" class="avatar" />
        </template>
        <template #title>
          <span class="username">{{ userName }}</span>
        </template>
        <template #label>
          <van-tag color="#fff" text-color="#007bff">申请认证</van-tag>
        </template>
      </van-cell>
      <!-- 动态、关注、粉丝 -->
      <div class="user-data">
        <div class="user-data-item" @click="dynamicFn">
          <span>{{ userInfoObj.art_count }}</span>
          <span>动态</span>
        </div>
        <div class="user-data-item" @click="followUpFn">
          <span>{{ userInfoObj.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="user-data-item">
          <span>{{ userInfoObj.fans_count }}</span>
          <span>粉丝</span>
        </div>
      </div>
    </div>

    <!-- 操作面板 -->
    <van-cell-group class="action-card">
      <van-cell icon="edit" title="编辑资料" is-link to="/user_edit" />
      <van-cell icon="chat-o" title="小思同学" is-link to="/chat" />
      <van-cell icon="star-o" title="收藏的文章" is-link to="/start_article" />
      <van-cell icon="warning-o" title="退出登录" is-link @click="signout" />
    </van-cell-group>
  </div>
</template>

<script>
import { Dialog } from 'vant';
import { getUserInfoApi } from '../../api/index.js';
import { removeToken } from '../../utils/token.js';
import { mapState } from 'vuex';
export default {
  name: 'User',
  data() {
    return {
      userInfoObj: {},
    };
  },
  computed: {
    ...mapState(['userAvatar', 'userName']),
  },
  activated() {
    this.getUserInfoFn();
  },
  methods: {
    // 获取用户基本信息
    async getUserInfoFn() {
      let { data: res } = await getUserInfoApi();
      console.log(res);
      this.userInfoObj = res.data;
      // 把用户头像存储到 vuex
      this.$store.dispatch('setphoto', res.data.photo);
      this.$store.commit('setUserName', res.data.name);
    },
    // 跳转到关注作者的组件
    followUpFn() {
      this.$router.push('/followe_up');
    },
    // 退出
    signout() {
      Dialog.confirm({
        title: '标题',
        message: '确认退出吗?',
      })
        .then(() => {
          removeToken();
          localStorage.removeItem('refresh');
          this.$router.replace('/login');
          this.$notify({ type: 'success', message: '退出成功~' });
        })
        .catch(() => {
          this.$notify({ type: 'danger', message: '取消操作' });
        });
    },
		// 动态粉丝事件
		dynamicFn() {
			this.$notify({type:'warning',message:'该功能尚未开发'})
		}
  },
};
</script>

   <style scoped lang="less">
.user-container {
  .user-card {
    background-color: #007bff;
    color: white;
    padding-top: 20px;
    .van-cell {
      background: #007bff;
      color: white;
      &::after {
        display: none;
      }
      .avatar {
        width: 60px;
        height: 60px;
        background-color: #fff;
        border-radius: 50%;
        margin-right: 10px;
      }
      .username {
        font-size: 14px;
        font-weight: bold;
      }
    }
  }
  .user-data {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    font-size: 14px;
    padding: 30px 0;
    .user-data-item {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 33.33%;
    }
  }
}

@media screen and (min-width: 500px) and (max-width: 1000px){
			.avatar-pd {
				padding: 10px;
			}
      .user-card {
        padding-top: 0!important;
      }
      .user-data {
        padding: 5px 0!important;
      }
		}
</style>
